<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .bg{
            background-color: blue;
        }
        #con{
            width: 400px;
            height: 300px;
            border: 2px solid red;
            padding: 5px 10px;
        }
    </style>
</head>
<body>
    <div id="con" class="list">
        <img src="images/fruit2.png" style="width: auto;">
    </div>
</body>
<script>

    var con = document.getElementsByClassName("list");
    
    //鼠标移到
    con[0].onmouseover = function(){
        // this.style.border = "2px solid blue";
        // this.className =this.className +  " bg";
        var classStr = this.className;
        var classAtt = classStr.split(" ");
        if(classAtt.indexOf("bg") >=0){
            return;
        }else{
            this.className = this.className + " bg";
        }
        
    }
    con[0].onmouseout = function(){
        var classStr = this.getAttribute("class");
        var classArr = classStr.split(" ");
       var index = classArr.indexOf("bg");
       if(index >= 0){
        classArr.splice(index);
        var str = classArr.join(" ");
        console.log(str);
        this.setAttribute("class",str);
       }else{
        return;
       }

    }
   
    
</script>
</html>